package com.example.springboot.mapper;

import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 功能
 * 作者：程序员小白
 * 日期：2024-09-22 10:36
 */
@Mapper
public interface UserMapper {

    @Insert("insert into `user` (username,password,name,phone,email,address,avatar)" +
            "values(#{username},#{password},#{name},#{phone},#{email},#{address},#{avatar})")
    void insertUser(User user);

    @Delete("delete from `user` where id = #{id}")
    void deleteUser(int id);

    @Update("update `user` set username = #{username}, password = #{password}, name = #{name}, " +
            "phone = #{phone}, email = #{email}, address = #{address}, avatar = #{avatar} where id = #{id}")
    void updateUser(User user);

    @Select("select * from `user` order by id desc")
    List<User> selectAllUser();

    @Select("select * from `user` where id = #{id}")
    User selectUserById(Integer id);

    @Select("select * from `user` where name = #{name}")
    List<User> selectUserByName(String name);

    @Select("select * from `user` where username like concat('%',#{username},'%') and " +
            "name like concat('%',#{name},'%') order by id desc")
    List<User> selectByMo(@Param("username") String username,@Param("name") String name);

    /*分页*/
    @Select("select * from `user` where username like concat('%',#{username},'%') and " +
            "name like concat('%',#{name},'%') order by id desc limit #{skipNum},#{pageSize}")
    List<User> selectByPage(@Param("skipNum")Integer skipNum,@Param("pageSize") Integer pageSize,
                            @Param("username") String username,@Param("name") String name);

    /*共有多少条数据*/
    @Select("select count(id) from `user` where username like concat('%',#{username},'%') and " +
            "name like concat('%',#{name},'%') order by id desc")
    int selectCountByPage(@Param("username") String username, @Param("name") String name);

    @Select("select * from `user` where username = #{username}")
    User selectByUserName(String username);

}
